<template>
  <j-modal title="特殊人群处置申请" :width="760" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="人员姓名" prop="personId">
        <a-select style="width:100%;" v-model="form.personId" :disabled="personId ? true : false" allowClear>
          <a-select-option v-for="(item, index) in personList" :value="item.id" :key="index"
            @click.native="nameChange(item)">{{ item.realName }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="身份证号" prop="identification">
        <a-input v-model="form.identification" :disabled="true" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="特殊人员类型" prop="specialPersonType">
        <a-select v-model="form.specialPersonType" placeholder="请选择" allowClear>
          <a-select-option value="1">病重</a-select-option>
          <a-select-option value="2">精神疾病</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="证明材料" prop="supportMaterials">
        <j-upload v-model="form.supportMaterials" :number="1"></j-upload>
        <p>支持扩展名：.jpg .png .pdf</p>
      </a-form-model-item>
      <a-form-model-item label="申请管控免除类型：" class="beforeRequiredSign">
        <a-checkbox-group v-model="controlExemptionType" @change="onCheckChange1" style="padding:10px 0 20px 0;">
          <a-checkbox value="1">打卡签到</a-checkbox>
          <a-checkbox value="2">常规尿液检测</a-checkbox>
          <a-checkbox value="3">突击尿液检测</a-checkbox>
          <a-checkbox value="4">定期访谈</a-checkbox>
        </a-checkbox-group>
      </a-form-model-item>
      <a-form-model-item label="免打卡签到时间" class="item1" v-if="checkV1 >= 0">
        <a-range-picker v-model="mdkTime" allowClear valueFormat="YYYY-MM-DD" @change="rangePickerChange1"
          style="width:100%" />
      </a-form-model-item>
      <a-form-model-item label="免常规尿液检测时间" class="item1" v-if="checkV2 >= 0">
        <a-select placeholder="请选择" mode="multiple" @change="selectChange1" v-model="njNArr">
          <a-select-option v-for="(item, key) in urineArr" :key="key" :value="item.detectNum">第{{
    item.detectNum
}}次;{{ item.urineStartTime }}~{{ item.urineEndTime }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="免突击尿液检测时间" class="item1" v-if="checkV3 >= 0">
        <a-range-picker v-model="mljTime" allowClear valueFormat="YYYY-MM-DD" @change="rangePickerChange2"
          style="width:100%" />
      </a-form-model-item>
      <a-form-model-item label="免定期访谈时间" class="item1" v-if="checkV4 >= 0">
        <a-select placeholder="请选择" mode="multiple" @change="selectChange2" v-model="dqNArr">
          <a-select-option v-for="(item, key) in interviewArr" :key="key" :value="item.interviewNum">第{{
    item.interviewNum
}}次;{{ item.interviewStartTime }}~{{ item.interviewEndTime }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="申请管控类型" class="beforeRequiredSign">
        <a-checkbox-group v-model="controlType" @change="onCheckChange2" style="padding:10px 0 20px 0;">
          <a-checkbox value="1">毛发检测</a-checkbox>
        </a-checkbox-group>
      </a-form-model-item>
      <template v-if="checkV6 > 0">
        <a-form-model-item :label="'第' + (index + 1) + '次毛发检测时间'" class="item1" v-for="(item, index) in itemArr"
          :key="index">
          <a-range-picker allowClear valueFormat="YYYY-MM-DD" v-model="timeArr[index]"
            @change="e => rangePickerChange3(e, index)" style="width:90%;margin-bottom: 10px;" />
          <img src="../img/del.png" v-if="index > 0" style="margin-left:10px; cursor: pointer;"
            @click="onDelete(index)" />
        </a-form-model-item>
        <a-button class="btn" style="width:61%;margin-left:29%" @click="onAdd()" type="dashed" block>+ 添加
        </a-button>
      </template>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as specialApi from '@/api/sjsk/specialPopul.js'
import * as uroscopyApi from '@/api/sjsk/check/uroscopy.js'
import * as interviewApi from '@/api/sjsk/help/interview.js'
import * as personServiceApi from '@/api/sjsk/personService.js'
export default {
  name: 'AddModal',
  props: ['personId'],
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        personId: [{ required: true, message: '请选择' }],
        identification: [{ required: true, message: '请输入' }],
        specialPersonType: [{ required: true, message: '请选择' }],
        supportMaterials: [{ required: true, message: '请选择' }],
      },
      loading: false,
      personList: [],
      typeArr: [],
      pageFilter: {
        pageNo: 1,
        pageSize: 1000
      },
      checkV1: '-1',
      checkV2: '-1',
      checkV3: '-1',
      checkV4: '-1',
      checkV6: '',
      itemArr: [1],
      timeArr: [],
      itemIndex: 0,
      sjskPersonId: "",
      urineArr: [],
      interviewArr: [],
      controlExemptionType: [],
      mdkTime: [],
      mljTime: [],
      controlType: [],
      isAdd: true,
      njNArr: [],
      dqNArr: []
    }
  },
  async created() { },
  methods: {
    show(record, type) {
      this.form = {}
      this.checkV1 = '-1'
      this.checkV2 = '-1'
      this.checkV3 = '-1'
      this.checkV4 = '-1'
      this.checkV6 = ''
      this.itemArr = [1] //个数数组
      this.timeArr = [] //时间数组
      this.sjskPersonId = ""
      this.urineArr = []
      this.interviewArr = []
      this.visible = true
      this.personList = []
      this.controlExemptionType = []
      this.controlType = []
      this.njNArr = []
      this.dqNArr = []
      this.getSJSK() //获取社戒社康人员
      // console.log("11111111")
      // console.log(record)
      if (record != undefined && type == undefined) { //这块是 "重新提交" 的功能，暂时删掉了
        this.isAdd = false
        this.getDetail(record.id)
      } else {
        this.isAdd = true
      }
    },
    getDetail(id) {
      specialApi.viewPopulDetails({ id: id }).then(res => {
        if (res.success) {
          this.form = res.result
          this.form.personId = Number(res.result.personId)
          this.form.identification = res.result.identification
          this.form.specialPersonType = res.result.specialPersonType.toString()
          this.controlExemptionType = res.result.controlExemptionType.split(',')//复选框选中
          this.checkV1 = this.controlExemptionType.indexOf("1")
          this.checkV2 = this.controlExemptionType.indexOf("2")
          this.checkV3 = this.controlExemptionType.indexOf("3")
          this.checkV4 = this.controlExemptionType.indexOf("4")
          this.controlType = res.result.controlType.split(',')//复选框选中
          if (this.controlType.length > 0) this.checkV6 = this.controlType.length

          //常规尿检
          if (res.result.mnjNum) {
            let njArr = res.result.mnjNum.split(',') //数量
            njArr.forEach(item => {
              this.njNArr.push(Number(item))
            })
          }
          //定期访谈
          if (res.result.mdqftNum) {
            let dqArr = res.result.mdqftNum.split(',') //数量
            dqArr.forEach(item => {
              this.dqNArr.push(Number(item))
            })
          }

          //毛发检测
          let hairArr1 = res.result.hairInspect.split(';')
          this.itemArr = []
          let hairArr2 = hairArr1.slice(0, hairArr1.length - 1) //删除最后一个元素
          hairArr2.forEach((item, index) => {
            this.itemArr.push(1) //放入个数，循环用
            let arr = item.split(',')
            this.timeArr[index] = [arr[0], arr[1]] //控件显示时间
            // console.log(this.timeArr[index])
          })

          this.mdkTime = [res.result.mdkStartTime, res.result.mdkEndTime] //免打卡签到时间
          this.mljTime = [res.result.mljStartTime, res.result.mljEndTime] //免突击尿液检测时间

          this.sjskPersonId = res.result.personId
          this.getUrineData() //定期尿检数据
          this.getInterviewData() //定期访谈数据
        }
      })
    },
    nameChange(e) {
      // console.log("3333333333")
      // console.log(e)
      this.form.name = e.realName
      this.form.identification = e.identityCard
      this.sjskPersonId = e.id
      // if (this.checkV2 > 0) {
      this.getUrineData() //定期尿检数据
      // }
      // if (this.checkV4 > 0) {
      this.getInterviewData() //定期访谈数据
      // }
    },
    getSJSK() {
      personServiceApi
        .getPersonList(this.pageFilter)
        .then(res => {
          if (res.success) {
            this.personList = res.result.records
            // 外面调用该弹窗【默认选中指定人员】
            if (this.personId) {
              let datas = this.personList.filter((item) => item.id === this.personId)
              let datasd = datas && datas.length > 0 ? datas[0] : {}
              this.$set(this.form, 'personId', datasd && Object.keys(datasd).length > 0 ? datasd.id : '')
              this.nameChange(datasd)
            }
          }
        })
        .catch(e => {
          this.$message.warn('请求失败！')
        })
    },
    getUrineData() {
      uroscopyApi.urineDataTospecial({ sjskPersonId: this.sjskPersonId, urineType: 1 }).then(res => {
        if (res.success) {
          this.urineArr = res.result
        }
      })
    },
    getInterviewData() {
      interviewApi.interviewRecordDataTospecial({ sjskPersonId: this.sjskPersonId }).then(res => {
        if (res.success) {
          this.interviewArr = res.result
        }
      })
    },
    selectChange1(e) {
      // console.log(e)
      this.form.mnjNum = e.join() //免常规尿检次数
      let str = ''
      e.forEach(item1 => {
        this.urineArr.forEach(item2 => {
          if (item1 == item2.detectNum) {
            str += item2.urineStartTime + ',' + item2.urineEndTime + ';'
          }
        })
      })
      this.form.mnjTime = str //免常规尿检时间
      console.log(this.form.mnjNum)
      console.log(this.form.mnjTime)
    },
    selectChange2(e) {
      this.form.mdqftNum = e.join() //定期访谈次数
      let str = ''
      e.forEach(item1 => {
        this.interviewArr.forEach(item2 => {
          if (item1 == item2.interviewNum) {
            str += item2.interviewStartTime + ',' + item2.interviewEndTime + ';'
          }
        })
      })
      this.form.mdqftTime = str //定期访时间
      console.log(this.form.mdqftNum)
      console.log(this.form.mdqftTime)
    },
    onCheckChange1(e) {
      console.log(e)
      this.checkV1 = e.indexOf("1")
      if (this.checkV1 < 0) {
        this.$set(this.form, 'mdkStartTime', '')
        this.$set(this.form, 'mdkEndTime', '')
        this.mdkTime = []
      }
      this.checkV2 = e.indexOf("2")
      if (this.checkV2 < 0) {
        this.form.mnjNum = ""
        this.form.mnjTime = ''
        this.njNArr = []
      }
      this.checkV3 = e.indexOf("3")
      if (this.checkV3 < 0) {
        this.$set(this.form, 'mljStartTime', '')
        this.$set(this.form, 'mljEndTime', '')
        this.mljTime = []
      }
      this.checkV4 = e.indexOf("4")
      if (this.checkV4 < 0) {
        this.form.mdqftNum = ""
        this.form.mdqftTime = ''
        this.dqNArr = []
      }
      this.form.controlExemptionType = e.join()
      // console.log(this.form.controlExemptionType)
      // if (this.checkV2 > 0) {
      //   this.getUrineData() //定期尿检数据
      // }
      // if (this.checkV4 > 0) {
      //   this.getInterviewData() //定期访谈数据
      // }
    },
    onCheckChange2(e) {
      console.log(e)
      this.checkV6 = e.length
      this.form.controlType = e.join()
      if (this.checkV6 <= 0) { //取消勾选，变量初始化
        this.itemArr = [1]
        this.timeArr = []
        this.form.hairInspect = ""
      }
    },
    onAdd() {
      this.itemArr.push(1)
    },
    onDelete(index) {
      this.itemArr.splice(index, 1)
      this.timeArr.splice(index, 1)
      // console.log(this.timeArr)
      this.form.hairInspect = ""
      this.form.hairInspectNum = this.timeArr.length
      this.timeArr.forEach(item1 => {
        this.form.hairInspect += item1.join() + ";"
      })
      console.log(this.form.hairInspectNum)
      console.log(this.form.hairInspect)
    },
    rangePickerChange1(date) {
      this.$set(this.form, 'mdkStartTime', date[0])
      this.$set(this.form, 'mdkEndTime', date[1])
    },
    rangePickerChange2(date) {
      this.$set(this.form, 'mljStartTime', date[0])
      this.$set(this.form, 'mljEndTime', date[1])
    },
    rangePickerChange3(e, index) {
      // console.log(e)
      // console.log(index)
      this.form.hairInspect = ""
      this.timeArr[index] = e
      // this.timeArr[index] = e.join('~')
      // console.log(this.timeArr)

      let str = ''
      this.timeArr.forEach((item1, index) => {
        str += (index + 1) + ','
        this.form.hairInspect += item1.join() + ";"
      })
      this.form.hairInspectNum = str.slice(0, str.length - 1) //删除最后一个逗号
      console.log(this.form.hairInspectNum)
      console.log(this.form.hairInspect)
      // this.hairInspect
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.form)
          if (this.form.controlExemptionType == undefined || this.form.controlExemptionType == '') {
            this.$message.warn("请选择申请管控免除类型")
            return
          }
          // console.log(this.checkV2)
          if (this.checkV1 >= 0) {
            if (this.form.mdkStartTime == "" || this.form.mdkStartTime == undefined) {
              this.$message.warn("免打卡签到时间不能为空")
              return
            }
          }
          if (this.checkV2 >= 0) {
            if (this.form.mnjTime == "" || this.form.mnjTime == undefined) {
              this.$message.warn("免常规尿液检测时间不能为空")
              return
            }
          }
          if (this.checkV3 >= 0) {
            if (this.form.mljStartTime == "" || this.form.mljStartTime == undefined) {
              this.$message.warn("免突击尿液检测时间不能为空")
              return
            }
          }
          if (this.checkV4 >= 0) {
            if (this.form.mdqftTime == "" || this.form.mdqftTime == undefined) {
              this.$message.warn("免定期访谈时间不能为空")
              return
            }
          }
          if (this.form.controlType == undefined || this.form.controlType == '') {
            this.$message.warn("请选择申请管控类型")
            return
          }
          // console.log(this.timeArr.length)
          // console.log(this.itemArr.length)
          if (this.form.hairInspect == "" || this.form.hairInspect == undefined) {
            this.$message.warn("请选择毛发检测时间")
            return
          } else {
            if (this.timeArr.length != this.itemArr.length) {
              this.$message.warn("请选择毛发检测时间")
              return
            }
          }
          this.loading = true
          let api = ""
          if (this.isAdd) {
            api = specialApi.apply(this.form)
          } else {
            api = specialApi.resubmit(this.form)
          }
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.visible = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.visible = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-col-5{
  width: 23%;
}
.item1 {
  width: 90%;
  margin-left: 10.5%;
  /deep/.ant-col-5 {
    label::before {
      display: inline-block;
      margin-right: 4px;
      color: #f5222d;
      font-size: 14px;
      font-family: SimSun, sans-serif;
      line-height: 1;
      content: '*';
    }
  }
}

.beforeRequiredSign {
  /deep/.ant-col-5 {
    label::before {
      display: inline-block;
      margin-right: 4px;
      color: #f5222d;
      font-size: 14px;
      font-family: SimSun, sans-serif;
      line-height: 1;
      content: '*';
    }
  }
}
</style>
